#include <iostream>
#include <memory>
//简单工厂模式,
//优点：简单易懂
//缺点：对开闭原则遵守的不是太好
class Fruit
{
public:
    virtual void name()=0;
};

class Apple:public Fruit
{
public:
    void name() override
    {
        std::cout<<"我是一个苹果"<<std::endl;
    }
};

class Banana:public Fruit
{
public:
    void name() override
    {
        std::cout<<"我是一个香蕉"<<std::endl;
    }
};

// class FruitFactory
// {
// public:
//     static std::shared_ptr<Fruit> create(std::string name)
//     {
//         if(name=="苹果")
//         {
//             return std::make_shared<Apple>();
//         }
//         else if(name=="香蕉")
//         {
//             return std::make_shared<Banana>();
//         }
//     }
// };
class FruitFactory
{
public:
    virtual std::shared_ptr<Fruit> create()=0;
};

class AppleFactory :public FruitFactory
{
public:
    std::shared_ptr<Fruit> create() override
    {
        return std::make_shared<Apple>();
    }
};
class BananaFactory :public FruitFactory
{
public:
    std::shared_ptr<Fruit> create() override
    {
        return std::make_shared<Banana>();
    }
};

int main()
{
    // std::shared_ptr<Fruit> Fusvr=FruitFactory::create("苹果");
    // Fusvr->name();
    // Fusvr=FruitFactory::create("香蕉");
    // Fusvr->name();
    std::shared_ptr<FruitFactory> ff(new AppleFactory);
    std::shared_ptr<Fruit> fruit=ff->create();
    fruit->name();

    ff.reset(new BananaFactory);
    fruit=ff->create();
    fruit->name();
    return 0; 
}